/*  Army Editor for yALB - Yet Another List Builder (roster Editor for Wargames/tabletop games).
    Copyright (C) 2008-2012  Daniel Ricardo Castro Alvarado

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.*/

#ifndef ARMYEDITOR_H
#define ARMYEDITOR_H

#include <QMainWindow>
#include <QTreeWidget>
#include <QActionGroup>
#include <QTranslator>

#include "widgets/itemselector.h"
#include "widgets/categorywidget.h"
#include "widgets/optionwidget.h"
#include "widgets/itemwidget.h"
#include "widgets/unitwidget.h"
#include "widgets/globaltextwidget.h"

#include "collections/categorycollection.h"
#include "collections/columncollection.h"
#include "collections/propertycollection.h"
#include "collections/optioncollection.h"
#include "collections/itemcollection.h"
#include "collections/unitcollection.h"
#include "collections/globaltextcollection.h"

#include "other/filestructs.h"
#include "other/dataeditormodels.h"
#include "other/dualmodel.h"

class CategorizerModel;

namespace Ui {
    class ArmyEditor;
}

class ArmyEditor : public QMainWindow
{
    Q_OBJECT

public:
    explicit ArmyEditor(QWidget *parent = 0);
    ~ArmyEditor();

protected:
    void closeEvent(QCloseEvent *event);
    void changeEvent(QEvent *event);

private slots:
    void initialize();
    void languageChanged(QAction *action);

    void on_actionNewArmyFile_triggered();
    void on_actionOpenArmyFile_triggered();
    void on_actionSaveArmyFile_triggered();
    void on_actionSaveArmyFileAs_triggered();
    void on_actionExit_triggered();

    void on_inheritTree_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous);
    void on_inheritSaveButton_clicked();

    inline void setUnsaved() { setSaved(false); }

    void addUnitCategory(const GameCategory &category, const QString &key);
    void clearUnitCategories();

    void addOptionCategory(const GameCategory &category, const QString &key);
    void clearOptionCategories();

    void addItemCategory(const GameCategory &category, const QString &key);
    void clearItemCategories();

    void addOption(const UnitOption& option, const QString &key);

    void addItem(const GameItem& item, const QString &key);

private:
    Ui::ArmyEditor *ui;

    QActionGroup *languageGroup;
    QTranslator qtTranslator;
    QTranslator editorTranslator;
    QString currentLanguage;

    ItemSelector *itemSelector;

    QString savedFile;
    bool isSaved;

    CategoryLimitDialog *categoryDialog;

    // Editor widgets

    CategoryWidget *unitCategoryWidget;
    CategoryWidget *itemCategoryWidget;
    CategoryWidget *optionCategoryWidget;
    OptionWidget *optionWidget;
    ItemWidget *itemWidget;
    UnitWidget *unitWidget;
    GlobalTextWidget *textWidget;

    // Data

    GameFileData gameData;
    ArmyFileData armyData;

    QHash<QString, InheritedCategoryData > inheritedUnitCategoryData;
    QHash<QString, InheritedCategoryData > inheritedItemCategoryData;
    QHash<QString, InheritedCategoryData > inheritedOptionCategoryData;
    QHash<QString, InheritedItemData > inheritedItemData;
    QHash<QString, InheritedOptionData> inheritedOptionData;

    // Models

    ArmyEditorModels armyModels;
    GameEditorModels gameModels;

    DualModel *dualUnitCategoryModel;
    DualModel *dualItemCategoryModel;
    DualModel *dualOptionCategoryModel;

    CategorizerModel *itemCategorizerModel;
    CategorizerModel *optionCategorizerModel;

    // Private methods

    void loadLanguage(QString language);

    void clearInheritanceForm();
    void resetInheritanceForm();

    void setSaved(bool saved);
    bool unsavedFileMessage();
    bool newArmyFile(const QString& gameFilePath);
    bool openArmyFile(const QString& armyFilePath, const QString& gameFilePath);
    bool saveArmyFile(const QString& path);

    void clearAll();

    void setWidgetSignalsConnected(bool value);
    void setInheritanceSignalsConnected(bool value);

    void addInheritanceItem(const QString& name, const QString &key, QTreeWidgetItem *parent);
    void removeInheritanceItem(const QString& key, QTreeWidgetItem *parent);
};

#endif // ARMYEDITOR_H
